<template>
	<view class="container">
		<view class="flex-just-between">
			<view class="card-info flex-item-center">
				<image src="/static/icon.png"></image>
				<view>oxf3*****kais03</view>
			</view>
		</view>
		

		<view class="title one">{{ $t('recharge.title') }}</view>
		<view class="record-box flex-just-between">
			<view class="record-info flex-item-center">
				<image src="@/static/money-icon.png"></image>
				<view>{{ $t('recharge.recordInfo') }}</view>
			</view>
			<view @click="jumpRechargeRecord">{{ $t('recharge.view') }}</view>
		</view>

		<view class="title">{{ $t('recharge.amountTitle') }}</view>
		<view class="sub-title">{{ $t('recharge.subTitle') }}</view>
		<view class="input-box">
			<input type="number" :placeholder="$t('recharge.inputPlaceholder')" v-model="inputAmount"
				@input="checkInput">
		</view>

		<view class="text-title flex-just-between">
			<view>{{ $t('recharge.walletBalanceTitle') }}</view>
			<view>{{ $t('recharge.authorizedTitle') }}</view>
		</view>
		<view class="money-list flex-item-center">
			<view class="amount-item" @click="setInputAmount('100.00')">$100.00</view>
			<view class="amount-item" @click="setInputAmount('250.00')">$250.00</view>
		</view>

		<view class="continue-btn" @click="toRecharge">{{ $t('recharge.continueBtn') }}</view>
	</view>
</template>

<script>
	import rechargeApi from '@/api/recharge.js';
	
	export default {
		data() {
			return {
				inputAmount: ''
			}
		},
		methods: {
			jumpRechargeRecord() {
				uni.navigateTo({
					url: '/pages/my/recharge-record/record'
				})
			},
			setInputAmount(amount) {
				this.inputAmount = amount;
			},
			checkInput(e) {
				const inputValue = e.target.value;
				// 只允许输入数字和小数点，且最多保留两位小数
				const validValue = inputValue.replace(/[^\d.]/g, '').replace(/(\..*)\./g, '$1');
				if (validValue.length > 0 && validValue.split('.').length > 1 && validValue.split('.')[1].length > 2) {
					this.inputAmount = validValue.slice(0, -1);
				} else {
					this.inputAmount = validValue;
				}
			},
			// 充值
			toRecharge() {
				rechargeApi.initiateRecharge({
					amount: this.inputAmount
				}).then(res => {
					uni.navigateTo({
						url: '/pages/my/recharge-record/record'
					})
				})
			}
		}
	}
</script>

<style lang="less" scoped>
	.container {
		padding: 0 40rpx;
		min-height: 90vh;
		background: #FBFCFF;

		.card-info {
			font-size: 32rpx;

			image {
				margin-right: 24rpx;
				width: 60rpx;
				height: 60rpx;
			}
		}

		.icon-box {
			image {
				width: 36rpx;
				height: 36rpx;

				&:last-child {
					margin-left: 38rpx;
					width: 38rpx;
					height: 32rpx;
				}
			}
		}

		.title {
			line-height: 78rpx;
			font-size: 36rpx;

			&.one {
				margin: 54rpx 0 26rpx;
			}
		}

		.record-box {
			margin-bottom: 120rpx;
			width: 100%;
			height: 128rpx;
			background-color: #52a5f3;
			border-radius: 20rpx;
			font-size: 32rpx;
			padding: 0 38rpx 0 26rpx;
			color: #fff;

			image {
				margin-right: 22rpx;
				width: 48rpx;
				height: 48rpx;
			}
		}

		.sub-title {
			color: #B7BAC2;
			font-size: 30rpx;
			line-height: 42rpx;
		}

		.input-box {
			margin-top: 44rpx;
			height: 112rpx;
			background-color: #fff;
			box-shadow: 20rpx 20rpx 80rpx 0rpx rgba(7, 17, 53, 0.04);
			border-radius: 20rpx;
			overflow: hidden;

			input {
				width: 100%;
				height: 100%;
				line-height: 112rpx;
				padding: 0 46rpx;
			}
		}

		.text-title {
			margin: 50rpx 0 54rpx;
			padding: 0 20rpx;
			color: #D9D9D9;
			font-size: 28rpx;
		}

		.money-list {
			height: 200rpx;
			flex-wrap: wrap;
			align-items: flex-start;

			view {
				margin-right: 20rpx;
				width: 176rpx;
				height: 72rpx;
				line-height: 72rpx;
				background: rgba(82, 165, 243, 0.1);
				color: #40BCFC;
				font-size: 28rpx;
				border-radius: 36rpx;
				text-align: center;
			}
		}

		.continue-btn {
			margin: 20rpx auto 0;
			width: 622rpx;
			height: 128rpx;
			line-height: 128rpx;
			background-color: #52a5f3;
			font-size: 40rpx;
			color: #fff;
			font-weight: bold;
			text-align: center;
			border-radius: 64rpx;
		}
	}
</style>